<?php

include 'inc/conn.php';

$id = '0';
$mode = 'a';
$packageid = 0;
$filename = '';
if (isset($_POST['id']))
    $id = $_POST['id'];

if(isset($_POST['mode']))
    $mode=$_POST['mode'];

if(isset($_POST['packageid']))
    $packageid = $_POST['packageid'];

if(isset($_POST['filename']))
    $filename = $_POST['filename'];

$tablename = "ats_package_images";
switch ($mode) {
    case 'a':
        //add, insert new image
        $sql = "insert into $tablename(packageid,filename) values('$packageid','$filename')";

        $rs = mysql_query($sql)
            or die('sql error');
        $newid = mysql_insert_id();
        echo $newid;
        break;
    case 'd':
        //delete image
        $sql = "select * from $tablename where id=$id";

        $res = mysql_query($sql);
        $row = mysql_fetch_row($res);
        if(file_exists($row[2]))
            unlink($row[2]);
        
        $sql = "delete from $tablename where id=$id";
        $rs = mysql_query($sql);
        echo $sql;
        break;
}
?>
